###############################################
#!file Makefile
#!author TooOld2Rock'nRoll (toooldtorock-n-roll@riseup.net)
#!date 2022
#!remark Last modified in 2023/oct
#!brief Makefile for the Arcade Fighter Library.
#!see - Mecklenburg, R.  - 2009 - Managing Projects with GNU Make (Nutshell Handbooks)
###############################################
SHELL := /bin/bash # Use bash syntax

#the output library name!
override OUT := libArcadeFighter.a

srcdir := src
incdir := inc
objdir := .obj
shadir := shaders
shatargetdir := $(incdir)/graphics/$(shadir)
create_dirs := $(objdir) $(shatargetdir)
.SUFFIXES: .vert .frag

#flags for the compiler
override CFLAGS += -g -Wall -Wpedantic -pthread -MP -MD
override CXXFLAGS += $(CFLAGS)
#where to look for header files
CPPFLAGS := -I$(incdir)
#how to pack static library
ARFLAGS	:= rs

#list all source files
vpath %.c $(srcdir)
vpath %.cpp $(srcdir)
c_files := $(wildcard $(srcdir)/*.c)
cpp_files := $(wildcard $(srcdir)/*.cpp)

#change path of .o files to the objdir target.
c_objs	:= $(patsubst $(srcdir)/%.c,$(objdir)/%.o,$(c_files))
cpp_objs:= $(patsubst $(srcdir)/%.cpp,$(objdir)/%.o,$(cpp_files))

#list all shader files that need to be transformed into header files
shader_files	:= $(wildcard $(shadir)/shader.*)
shader_headers	:= $(patsubst $(shadir)/%,$(shatargetdir)/%.hpp,$(shader_files))

#create necessary directories if not created yet
create-output-directories := \
	$(shell for d in $(create_dirs); \
		do [[ -d $$d ]] || mkdir -p $$d; \
	done)

# $(info SRC_LIST: $(c_files) $(cpp_files) $(shader_headers))
#$(info OBJ_LIST: $(c_objs) $(cpp_objs) $(shader_dep))


#*******************************
#********build targets**********
#*******************************
.PHONY: all mem_profiling clean
#.PHONY: help put this help up there when ready....

# help - The default goal
#TODO make a proper help!!!
#help:
	#@$(MAKE) --print-data-base --question no-such-target /
	#$(GREP) -v -e '^no-such-target' -e '^makefile' /
	#$(AWK) '/^[^.%][-A-Za-z0-9_]*: { print substr($$1, 1, length($$1)-1) }' /
	#$(SORT) /
	#$(PR) --omit-pagination --width=80 --columns=4


all: $(OUT)

#change flags depending on target!
mem_profiling: CFLAGS += -fsanitize=address -fno-omit-frame-pointer
mem_profiling: all

#pack the library and all necessary files
$(OUT): $(c_objs) $(cpp_objs)
	$(AR) $(ARFLAGS) $@ $?
	ranlib $@

#make .o into .d files and set as dependency (to recompile on header files changes)
#clean does not need to generate a list of dependencies!
ifneq "$(MAKECMDGOALS)" "clean"
  -include $(c_objs:.o=.d) $(cpp_objs:.o=.d)
endif


#compile .c files
$(c_objs): $(shader_headers)
	$(COMPILE.c) $(OUTPUT_OPTION) $(patsubst $(objdir)/%.o,$(srcdir)/%.c,$@)

#compile .cpp files
$(cpp_objs): $(shader_headers)
	$(COMPILE.cc) $(OUTPUT_OPTION) $(patsubst $(objdir)/%.o,$(srcdir)/%.cpp,$@)

#transform shader files into .hpp files so they can be shipped with the library
$(shader_headers): $(shader_files)
	$(file > $@,R""($(file < $(patsubst $(shatargetdir)/%.hpp,$(shadir)/%,$@)))"")

#remove all files generated by make
clean:
	rm -rf $(shatargetdir)/*
	rm -rf $(objdir)/*
	rm -f $(OUT)

